import {
  getAddress,
  getChinaList,
  addAddress,
  updateAddress
} from '../../api/addressApi.js'


export default {
  namespaced: true,
  state: {
    //添加地址是否显示
    vs: false,
    //当前用户的收货地址列表
    address_list: [],
    //省
    china_list: [],
    //市
    city_list: [],
    //区县
    district_list: [],
    //收货人姓名
    uaddr_name: '',
    //收货人手机号码
    uaddr_phone: '',
    //收获详细地址
    uaddr_address: '',
    //省
    uaddr_province: '北京市',
    //市
    uaddr_city: '东城区',
    //区县
    uaddr_district: '',
    //保存默认地址
    default_address: {}
  },
  getters: {},
  mutations: {
    //设置当前用户的收货地址列表
    set_address_list(state, value) {
      state.address_list = value;
    },
    //设置添加地址是否显示
    set_vs(state, value) {
      state.vs = value
    },
    //设置省
    set_china_list(state, value) {
      state.china_list = value
    },
    //市
    set_city_list(state, value) {
      state.city_list = value
    },
    //区县
    set_district_list(state, value) {
      state.district_list = value
    },
    //设置默认地址
    set_default_address(state, value) {
      state.default_address = value
    }
  },
  actions: {
    //地址列表
    get_address_list(context) {
      getAddress({}).then(function(response) {
        if (response.data.httpcode == 200) {
          context.commit('set_address_list', response.data.data);
          response.data.data.forEach(function(addr) {
            if (addr.uaddr_isdefault == 1) {
              context.commit('set_default_address', addr);
            }
          })
          /* const addr = response.data.data.find(function(addr){
            return addr.uaddr_isdefault == 1
          })
          context.commit('set_default_address', addr); */
        }
      })
    },
    //省市区列表
    get_china_list(context, value) {
      getChinaList(value).then(function(response) {
        if (response.data.httpcode == 200) {
          context.commit('set_china_list', response.data.data);
          context.commit('set_city_list', response.data.data[0].children);
          context.commit('set_district_list', response.data.data[0].children[0].children);
        }
      })
    },
    //保存收货地址
    add_address(context) {
      const addr = {
        uaddr_name: context.state.uaddr_name,
        uaddr_phone: context.state.uaddr_phone,
        uaddr_address: context.state.uaddr_address,
        uaddr_province: context.state.uaddr_province,
        uaddr_city: context.state.uaddr_city,
        uaddr_district: context.state.uaddr_district,
        uaddr_isdefault: 0
      }
      addAddress(addr).then(function(response) {
        if (response.data.httpcode == 200) {
          //重写获取地址列表
          context.dispatch('get_address_list');
          //设置添加页面隐藏
          context.commit('set_vs', false);
        }
      })
    },
    //修改收货地址
    update_address(context, value) {
      console.log(value)
      updateAddress(value).then(function(response) {
        if (response.data.httpcode == 200) {
          //重写获取地址列表
          context.dispatch('get_address_list');
        }
      })
    }
  }
}
